'use strict';

let nunjucks = require("nunjucks");

function createEnv(path,opts){
    path = path || "";
    opts = opts || {};

    let envOptions = {
        watch:opts.watch === undefined? true : opts.watch,
        autoescape: opts.autoescape === undefined ? true : opts.autoescape,
        noCach: opts.noCach === undefined ? true : opts.noCach,
        throwOnUndefind: opts.throwOnUndefind === undefined ? true : opts.throwOnUndefind,
    }

    let env = nunjucks.configure(path,envOptions)
    return env;
}

module.exports = async(ctx,next)=>{
    ctx.render = function(view,modle){
        let env = createEnv("view",{autoescape:false});
        ctx.body = env.render(view,modle);
    }
    await next();
}